package cn.itcast.com.homework.day724;

class FirstLevel implements Cloneable {
    //成员变量
    int firstIntValue;
    double firstDoubleValue;
    SecondLevel second;
    //构造方法
    public FirstLevel(int firstIntValue, double firstDoubleValue, SecondLevel second) {
        this.firstIntValue = firstIntValue;
        this.firstDoubleValue = firstDoubleValue;
        this.second = second;
    }

    //重写clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //利用Object的Clone方法，
        FirstLevel first = (FirstLevel) super.clone();

        //修改 first对象中引用类型成员变量，比如 second 成员变量引用，指向被复制的那个FirstLevel对象的second对象的拷贝，
        first.second = (SecondLevel) first.second.clone();
        return first;
    }

    public void testProtected() throws CloneNotSupportedException {
        //跨包子类访问父类 ，访问FirstLevel自己从Object继承下来的Clone方法
        clone(); //alt + enter

        FirstLevel firstLevel = new FirstLevel(10,9.9, second);
        firstLevel.clone();
    }
}

class SecondLevel implements Cloneable{
    int secondIntValue;
    double secondDoubleValue;
    ThirdLevel third;

    public SecondLevel(int secondIntValue, double secondDoubleValue,
                       ThirdLevel third) {
        this.secondIntValue = secondIntValue;
        this.secondDoubleValue = secondDoubleValue;
        this.third = third;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        SecondLevel clone = (SecondLevel) super.clone();
        clone.third = (ThirdLevel) third.clone();

        //修改SecondLevel这个对象中，引用类型的成员变量的值，让他指向，复制之后的成员变量所指向对象地址
        return clone;
    }
}

class ThirdLevel implements Cloneable{
    int thirdIntValue;
    double thirdDouleValue;

    public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
        this.thirdIntValue = thirdIntValue;
        this.thirdDouleValue = thirdDouleValue;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}